// pages/placing/rating.js
var util = require('../../network_util.js');
var config = require('../../config.js');
var commonjs = require('../../common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    driverTags: ['态度好', '很专业', '有礼貌', '很热情', '车技娴熟', '帅呆了',],
    appTags: ['使用方便', '页面美观', '价格合理', '32个赞', '吊炸天', '蓝爸爸',],
    driverTagsState: [],
    appTagsState: [],
    driverInfo: {},
    userInfo: {},
    orderId: 0,
    driverStar: 5,
    appStar: 5,
    driverStarTitle: ['很差', '不满意', '一般满意', '比较满意', '非常满意',],
    appStarTitle: ['很差', '不满意', '一般满意', '比较满意', '非常满意',],
    driverRemark:"",
    appRemark: "",
    driverAPIName: "司机标签",
    appAPIName: "APP标签"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取用户信息
    this.setData({ userInfo: wx.getStorageSync("userInfo") });

    this.setData({ orderId: options.orderId });
    // this.setData({ orderId: "504686752022186053" });
    

    this.getDriverTags();
    this.getAppTags();
    this.getDriverInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getDriverInfo: function(){
    var that = this;
    util._get(config.getDrivernfo + "?orderId=" + this.data.orderId + "&userId=" + this.data.userInfo.id, 
    function(res){
      console.log("driverInfo: ");
      console.log(res);
      that.setData({ driverInfo: res.data.data});
    })
  },
  driverTagTap: function (e) {
    console.log(e);
    // console.log(this.data.driverTagsState[e.currentTarget.dataset.index]);
    var tagsState = this.data.driverTagsState;
    var count = 0;
    for (var i = 0; i < tagsState.length; i++) {
      if (tagsState[i] == true) {
        count++;
      }
    }
    if (count >= 3) {
      if (tagsState[e.currentTarget.dataset.index] == false) {
        wx.showToast({
          title: '最多只能选三项',
        })
        return;
      }
    }
    tagsState[e.currentTarget.dataset.index] = !tagsState[e.currentTarget.dataset.index];
    this.setData({ driverTagsState: tagsState });
    // console.log(this.data.driverTagsState);
  },
  appTagTap: function (e) {
    console.log(e);
    var tagsState = this.data.appTagsState;
    var count = 0;
    for (var i = 0; i < tagsState.length; i++) {
      if (tagsState[i] == true) {
        count++;
      }
    }
    if (count >= 3) {
      if (tagsState[e.currentTarget.dataset.index] == false) {
        wx.showToast({
          title: '最多只能选三项',
        })
        return;
      }
    }
    tagsState[e.currentTarget.dataset.index] = !tagsState[e.currentTarget.dataset.index];
    this.setData({ appTagsState: tagsState });

  },  
  onDriverRemarkInput: function(e){
    this.setData({driverRemark: e.detail.value});
  },
  onAppRemarkInput: function (e) {
    this.setData({ appRemark: e.detail.value });
  },
  onSubmit: function(){
    var driverDetails = "";
    for (var i = 0; i < this.data.driverTagsState.length; i++){
      if (this.data.driverTagsState[i]){
        driverDetails = driverDetails + this.data.driverTags[i] + ",";
      }
    }
    var appDetails = "";
    for (var i = 0; i < this.data.appTagsState.length; i++) {
      if (this.data.appTagsState[i]) {
        appDetails = appDetails + this.data.appTags[i] + ",";
      }
    }
    util._post_json(config.orderComment,
      {
        uId: this.data.userInfo.id,
        driverId: this.data.driverInfo.id,
        orderId: this.data.orderId,
        driverService: this.data.driverStar,
        driverRemark: this.data.driverRemark,
        driverDetails: commonjs.trim(driverDetails, ',', 'right'),
        appExperience: this.data.appStar,
        appRemark: this.data.appRemark,
        appDetails: commonjs.trim(appDetails, ',', 'right')
      },
    function(res){
      if (res.statusCode == 200) {
        if (res.data.code == 200) {
          // that.setData({ guiderInfo: res.data.data });
          wx.showToast({
            title: '评价成功',
          })
          wx.redirectTo({
            url: '/pages/mine/trips',
          })
        } else {
          wx.showToast({
            title: '提交评价出错(' + res.data.code + ')',
          })
        }
      } else {
        //网络错误
        wx.showToast({
          title: '提交评价发生系统错误(' + res.statusCode + ')',
        })
      }
    }, function(){
      console.log("rating fail..");
    })

  },
  onDriverStarTap: function(e){
    var star = e.currentTarget.dataset.index;
    this.setData({driverStar: star});
    //请求对应tag
    this.getDriverTags();
  },
  getDriverTags: function () {
    var that = this;
    util._get(config.getStarRemark + "?type=1&star=" + this.data.driverStar,
      function (res) {
        if (res.statusCode == 200) {
          if (res.data.code == 200) {
            //成功
            that.setData({ driverTags: res.data.data.content.split(',') });
            var tagsState = new Array();
            for (var i = 0; i < that.data.driverTags.length; i++) {
              tagsState.push(false);
            }
            that.setData({ driverTagsState: tagsState });
            console.log(that.data.driverTagsState);
          } else {
            //接口错误
            wx.showToast({
              title: '获取' + that.data.driverAPIName + '出错(' + res.data.code + ')',
            })
          }
        } else {
          //系统错误
          wx.showToast({
            title: '获取' + that.data.driverAPIName + '发生系统错误(' + res.statusCode + ')',
          })
        }
      })
  },
  onAppStarTap: function (e) {
    var star = e.currentTarget.dataset.index;
    this.setData({ appStar: star });
    //请求对应tag
    this.getAppTags();
  },
  getAppTags: function () {
    var that = this;
    util._get(config.getStarRemark + "?type=2&star=" + this.data.appStar,
      function (res) {
        if (res.statusCode == 200) {
          if (res.data.code == 200) {
            //成功
            that.setData({ appTags: res.data.data.content.split(',') });
            var tagsState = new Array();
            for(var i=0; i< that.data.appTags.length; i++){
              tagsState.push(false);
            }
            that.setData({ appTagsState: tagsState});
          } else {
            //接口错误
            wx.showToast({
              title: '获取' + that.data.appAPIName + '出错(' + res.data.code + ')',
            })
          }
        } else {
          //系统错误
          wx.showToast({
            title: '获取' + that.data.appAPIName + '发生系统错误(' + res.statusCode + ')',
          })
        }
      })
  }
})